package com.prp.bo;

import org.hibernate.HibernateException;

import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IRecommendationEntityDAO;
import com.prp.db.idao.ISubjectChildCategoryDAO;
import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.RecommendationEntity;
import com.prp.db.po.SubjectChildCategory;
import com.prp.db.po.SubjectRecommendation;
import com.prp.db.po.UserInformation;

public class SubjectPublishingBO {
	private SubjectRecommendation subject;

	public static final int CONTENT_SIZE = SNSConstants.SubjectRocommendation_Content_Size;
	public static final int TITLE_SIZE = SNSConstants.SubjectRocommendation_SubjectTitle_Size;

	public SubjectPublishingBO() {
		subject = new SubjectRecommendation();
	}

	private void setCategoryChildId(int categoryChildId) {
		if (validateCategoryChildId(categoryChildId))
			subject.setCategoryChildId(categoryChildId);
	}

	private boolean validateCategoryChildId(int categoryChildId) {
		ISubjectChildCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectChildCategoryDAO();
		SubjectChildCategory scc = dao.findById(categoryChildId);
		if (scc == null)
			throw new IllegalArgumentException(
					"invalid category_child_id : < 0 or the child category does not exist");
		return true;
	}

	private void setContent(final String content) {
		if (content == null || content.trim().length() == 0)
			throw new IllegalArgumentException("content should not be empty");
		if (content.length() > CONTENT_SIZE)
			throw new IllegalArgumentException("content too long");

		subject.setContent(content);
	}

	public void publish(int categoryChildId, final String subjectTitle,
			int publishId, final String content, int recommendEntityId,
			final String pictureRoutine) {
		setProperties(categoryChildId, subjectTitle, publishId, content,
				recommendEntityId, pictureRoutine);
		saveSubject();
	}

	private boolean saveSubject() {
		ISubjectRecommendationDAO dao = DAOFactory.getDAOFactory()
				.getSubjectRecommendationDAO();
		org.hibernate.Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			dao.save(subject);
			tx.commit();

			return true;
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return false;
		}
	}

	private void setProperties(int categoryChildId, final String subjectTitle,
			int publishId, final String content, int recommendEntityId,
			final String pictureRoutine) {
		setCategoryChildId(categoryChildId);
		setSubjectTitle(subjectTitle);
		setPublishId(publishId);
		setContent(content);
		setRecommendEntityId(recommendEntityId);
		subject.setDate(new java.util.Date());
		subject.setClickFreq(0);
		subject.setReplyNum(0);
		subject.setWaitingTime(0);
	}

	private void setPublishId(int publishId) {
		if (validatePublishId(publishId))
			subject.setPublishId(publishId);
	}

	private boolean validatePublishId(int publishId) {
		IUserInformationDAO dao = DAOFactory.getDAOFactory()
				.getUserInformationDAO();
		UserInformation user = dao.findById(publishId);
		if (user == null)
			throw new IllegalArgumentException(
					"invalid publish id. <0 or user does not exist");
		if (!user.getActivated())
			throw new IllegalArgumentException("user has not activated yet.");
		return true;
	}

	private void setRecommendEntityId(int recommendEntityId) {
		if (validateRecommendEntityId(recommendEntityId))
			subject.setRecommendEntityId(recommendEntityId);
	}

	private boolean validateRecommendEntityId(int recommendEntityId) {
		IRecommendationEntityDAO dao = DAOFactory.getDAOFactory()
				.getRecommendationEntityDAO();
		RecommendationEntity re = dao.findById(recommendEntityId);
		if (re == null)
			throw new IllegalArgumentException(
					"invalid recommend_entity_id: <0 or does not exist");
		return true;
	}

	private void setSubjectTitle(final String subjectTitle) {
		if (subjectTitle == null || subjectTitle.trim().length() == 0)
			throw new IllegalArgumentException(
					"invalid subject_title: to short");
		if (subjectTitle.length() > TITLE_SIZE)
			throw new IllegalArgumentException("invalid subject_title: to long");

		subject.setSubjectTitle(subjectTitle);
	}

	public static void main(String[] args){
		SubjectPublishingBO bo = new SubjectPublishingBO();
		bo.publish(1, "title", 1, "content", 1, "");
	}
}
